//
//  GCDTest.m
//  MacTestProject
//
//  Created by LiuLichen on 2022/5/17.
//

#import "GCDTest.h"

@implementation GCDTest

- (id) init {
    self = [super init];

    [self testCreateDispatchQueue];
    [self testSystemDispatchQueue];
    [self testDispatchAfter];
    [self testDispatchGroup];
    [self testDispatchApply];

    return self;
}

- (void) testCreateDispatchQueue {
    dispatch_queue_t mySerialDispatchQueue = dispatch_queue_create("com.llc.dev.test.gcd.mySerialDispatchQueue", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t myConcurrentDispatchQueue = dispatch_queue_create("com.llc.dev.test.gcd.myConcurrentDispatchQueue", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(myConcurrentDispatchQueue, ^{NSLog(@"block on myConcurrentDispatchQueue");});
//    dispatch_release(myConcurrentDispatchQueue);
}

- (void) testSystemDispatchQueue {
    dispatch_queue_t mainDispatchQueue = dispatch_get_main_queue();
    dispatch_queue_t globalDispatchQueueHigh = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
    dispatch_queue_t globalDispatchQueueDefault = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_queue_t globalDispatchQueueLow = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
    dispatch_queue_t globalDispatchQueueBackground = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

    dispatch_async(globalDispatchQueueDefault, ^{
        /**
         * concurrent operations
         */

        dispatch_async(mainDispatchQueue, ^{
            NSLog(@"operate in main queue");
        });
    });
}

- (void) testDispatchAfter {
    dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 3ull * NSEC_PER_SEC);
    dispatch_after(time, dispatch_get_main_queue(), ^{
        NSLog(@"waited at least three seconds.");
    });
}

- (void) testDispatchGroup {
    {
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_async(group, queue, ^{NSLog(@"blk0");});
        dispatch_group_async(group, queue, ^{NSLog(@"blk1");});
        dispatch_group_async(group, queue, ^{NSLog(@"blk2");});

        dispatch_notify(group, dispatch_get_main_queue(), ^{NSLog(@"done");});
        //    dispatch_release(group);
    }
    {
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_group_t group = dispatch_group_create();
        dispatch_group_async(group, queue, ^{NSLog(@"blk0");});
        dispatch_group_async(group, queue, ^{NSLog(@"blk1");});
        dispatch_group_async(group, queue, ^{NSLog(@"blk2");});

        long result = dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        //    dispatch_release(group);
        if (result == 0) {
            NSLog(@"do something after the group is done");
        } else {
            NSLog(@"do something before the group is done");
        }
    }
}

- (void) testDispatchApply {
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_apply(10, queue, ^(size_t index) {
        NSLog(@"%zu", index);
    });
    NSLog(@"done");
}

@end
